package com.imooc.utils;

import cn.hutool.crypto.SecureUtil;
import com.imooc.annotation.EncryptionData;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author gzj
 * @since 2023/11/21
 */
public class ESUtil {

    private static final byte[] KEYS = "165496894sdfsgdf".getBytes(StandardCharsets.UTF_8);

    /**
     * es数据新增 加密
     * @param t
     * @return
     * @param <T>
     */
    public static <T> T saveData(T t){
        return dataEncrypt(t);
    }

    /**
     * es数据新增加密
     * @param t
     * @return
     * @param <T>
     */
    public static <T> List<T> saveData(List<T> t){
        for (T data : t) {
            dataEncrypt(t);
        }
        return t;
    }

//    /**
//     * es查询处理结果 List
//     * @param searchHits
//     * @return
//     * @param <T>
//     */
//    public static <T> List<T> getList(SearchHits<T> searchHits) {
//        List<SearchHit<T>> hits = searchHits.getSearchHits();
//        List<T> list = new ArrayList<>();
//        for (SearchHit<T> searchHit : hits) {
//            list.add(getOne(searchHit));
//        }
//        return list;
//    }
//    /**
//     * es查询处理结果 T
//     * @param searchHit
//     * @return
//     * @param <T>
//     */
//    public static <T> T getOne(SearchHit<T> searchHit) {
//        return (T) dataDecrypt(searchHit.getContent());
//    }



    /**
     * 解密
     * @param value
     * @return
     */
    public static String decrypt(String value){
        return SecureUtil.aes(KEYS).decryptStr(value);
    }


    /**
     * 加密
     * @param value
     * @return
     */
    public static String encrypt(String value){
        return SecureUtil.aes(KEYS).encryptHex(value);
    }

    /**
     * 数据解密
     * @param t
     * @return
     * @param <T>
     */
    private static  <T> T dataDecrypt(T t){
        Field[] fields = t.getClass().getDeclaredFields();
        int startNum = 0;
        for (Field field : fields) {
            try {
                Annotation[] anns = field.getAnnotations();
                EncryptionData encryption = null;
                for (Annotation ann : anns) {
                    if (ann instanceof EncryptionData) {
                        encryption = (EncryptionData) ann;
                    }
                }

                if (null != encryption) {
                    field.setAccessible(true);
                    String value = (String) field.get(t);
                    field.set(t, decrypt(value));
                }
            } catch (IllegalAccessException ignored) {
            }
        }
        return t;
    }

    /**
     * 数据加密
     * @param t
     * @return
     * @param <T>
     */
    private static  <T> T dataEncrypt(T t){
        Field[] fields = t.getClass().getDeclaredFields();
        int startNum = 0;
        for (Field field : fields) {
            try {
                Annotation[] anns = field.getAnnotations();
                EncryptionData encryption = null;
                for (Annotation ann : anns) {
                    if (ann instanceof EncryptionData) {
                        encryption = (EncryptionData) ann;
                    }
                }

                if (null != encryption) {
                    field.setAccessible(true);
                    String value = (String) field.get(t);
                    field.set(t, encrypt(value));
                }
            } catch (IllegalAccessException ignored) {
            }
        }
        return t;
    }
}
